package com.otod.pm.prj.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.otod.base.exception.AppException;
import com.otod.base.json.JsonSuccess;
import com.otod.commons.utils.StringUtils;
import com.otod.commons.web.BaseController;
import com.otod.pm.prj.bo.StepTpBo;
import com.otod.pm.prj.service.StepTpService;
import com.otod.pm.prj.service.TaskTpService;
import com.otod.pm.prj.vo.StepTpVo;

/**
 * 阶段管理
 * @author jqiu 
 */
@Controller
@RequestMapping("pm/stepTp")
public class StepTpController extends BaseController {

	@Autowired
	private StepTpService stepTpService;
	@Autowired
	private TaskTpService taskTpService;
	

	/**
	 * 保存阶段
	 * @param stepVo
	 * @return
	 */
	@RequestMapping(value ="save", method = RequestMethod.POST)
	@ResponseBody
	public StepTpVo save(StepTpVo stepTpVo,HttpServletRequest request) {
		if(StringUtils.isEmpty(stepTpVo.getUuid())){
			super.setInsertOperInfo(stepTpVo);
			return stepTpService.insert(stepTpVo);
		}else{
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("uuid", stepTpVo.getUuid());
			map.put("stepName", stepTpVo.getStepName());
			super.setUpdateOperInfo(map);
			stepTpService.update(map);
			return stepTpVo;
		}
	}
	
	/**
	 * 阶段查询
	 * @param prjId
	 * @return
	 * @throws AppException 
	 */
	@RequestMapping(value ="list", method = RequestMethod.POST)
	@ResponseBody
	public List<StepTpBo> list(@RequestBody String tpId) throws AppException{
		if(StringUtils.isEmpty(tpId)){
			throw new AppException("模板ID不能为空！");
		}
		List<StepTpVo> list = stepTpService.queryValidList(tpId);
		List<StepTpBo> listStepBo = new ArrayList<StepTpBo>();
		StepTpBo bo;
		for (StepTpVo stepTpVo : list) {
			bo = new StepTpBo();
			BeanUtils.copyProperties(stepTpVo, bo);
			bo.setTaskList(taskTpService.queryValidList(stepTpVo.getUuid()));
			listStepBo.add(bo);
		}
		return listStepBo;
	}
	
	/**
	 * 阶段删除
	 * @param uuid
	 * @return
	 */
	@RequestMapping(value ="delete", method = RequestMethod.POST)
	@ResponseBody
	public String delete(@RequestBody String uuid){
		stepTpService.deleteById(uuid);
		return JsonSuccess.factory("删除成功");
	}
}
